#!/usr/bin/env python3
"""
AI日志分析引擎测试运行脚本
"""

import sys
import os
import subprocess
import glob

def run_test_file(test_file):
    """运行单个测试文件"""
    print(f"[TEST] 运行测试: {test_file}")
    print("-" * 40)
    
    try:
        result = subprocess.run(
            [sys.executable, test_file],
            capture_output=True,
            text=True,
            timeout=300,
            encoding='utf-8',
            errors='ignore'
        )
        
        if result.returncode == 0:
            print("[PASS] 测试通过")
        else:
            print("[FAIL] 测试失败")
            print(f"错误输出: {result.stderr}")
        
        print(f"输出: {result.stdout}")
        print("=" * 50)
        return result.returncode == 0
        
    except subprocess.TimeoutExpired:
        print("⏰ 测试超时")
        return False
    except Exception as e:
        print(f"[FAIL] 运行测试时出错: {e}")
        return False

def main():
    """运行所有测试"""
    print("[TEST] AI日志分析引擎测试套件")
    print("=" * 50)
    
    # 获取所有测试文件
    test_files = glob.glob("tests/test_*.py")
    
    if not test_files:
        print("[FAIL] 没有找到测试文件")
        return
    
    print(f"[FILES] 找到 {len(test_files)} 个测试文件:")
    for test_file in test_files:
        print(f"   - {test_file}")
    
    print("\n[START] 开始运行测试...")
    
    passed = 0
    failed = 0
    
    for test_file in test_files:
        if run_test_file(test_file):
            passed += 1
        else:
            failed += 1
    
    print("\n" + "=" * 50)
    print("[STATS] 测试结果汇总:")
    print(f"[PASS] 通过: {passed}")
    print(f"[FAIL] 失败: {failed}")
    print(f"[REPORT] 总计: {passed + failed}")
    
    if failed == 0:
        print("\n[SUCCESS] 所有测试通过!")
    else:
        print(f"\n[WARN]  有 {failed} 个测试失败")

if __name__ == '__main__':
    main() 